<?php
class Admin_UserController extends Admin_Libraries_Controller_Action {
	
	protected $_config = null;
	
	public function init() {
		parent::init();
		$this->_config = Zend_Registry::get('configuration');		
	}
	
	public function indexAction() {		
		if (!$this->_acl->isUserAllowed('admin:user', 'view')) 
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jform.js', 'text/javascript');
		
		$arrWhere = array();
		$filterActive = $this->getRequest()->getParam('filter_active', '*');
		$filterRole = $this->getRequest()->getParam('filter_role', '*');
		$filterKeyword = $this->getRequest()->getParam('filter_keyword', '');
		if ($filterKeyword != '') $arrWhere[] = '(fullname like "%'. $filterKeyword .'%" 
												  OR username like "%'. $filterKeyword .'%"
												  OR email like "%'. $filterKeyword .'%")';		
		if ($filterRole != '*') $arrWhere[] = 'role_id=' . $filterRole; 
		if ($filterActive != '*') $arrWhere[] = 'active=' . $filterActive;		
		
		$cUser = new Admin_Model_User();
		$arrUtil = new Core_Utilities_Array();
		$this->view->arrUser = $cUser->getList($arrWhere);
		$this->view->arrRole = $arrUtil->buildArrayWithCustomKey($cUser->getListRole(), 'role_id');	
		$this->view->filterRole = $filterRole;
		$this->view->filterActive = $filterActive;	
		$this->view->filterKeyword = $filterKeyword;
	}
	
	public function addAction() {
		if (!$this->_acl->isUserAllowed('admin:user', 'add'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		$this->view->headScript()->appendFile( $this->_config->site->admin->jsUrl.'jform.js', 'text/javascript');
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		$cUser = new Admin_Model_User();
		$arrUtil = new Core_Utilities_Array();
		if ($this->getRequest()->isPost()) {
			$post = $this->getRequest()->getPost();
			$notEmpty = new Zend_Validate_NotEmpty();
			$isValid = TRUE;
			if (!$notEmpty->isValid($post['jform']['username'])
					|| $cUser->isExistValue($post['jform']['username'])) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['email'])
					|| $cUser->isExistValue($post['jform']['email'], 'email')) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['fullname'])) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['role'])) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['password'])
					|| $post['jform']['password'] != $post['jform']['comfirm_password']) $isValid = FALSE;
			if ($isValid) {
				$data = array();
				$data['username'] = $post['jform']['username'];
				$data['fullname'] = $post['jform']['fullname'];
				$data['password'] = md5($post['jform']['password']);
				$data['role_id'] = $post['jform']['role'];
				$data['email'] = $post['jform']['email'];
				$data['phone'] = $post['jform']['phone'];
				$data['active'] = $post['jform']['active'];			
				$response = $cUser->insert($data);
				if ($response > 0) {
					$flashMessage->addMessage(array('message'=>$this->view->translate('admin_save_successfull')));
					if ($post['save'] == 'save') $this->_redirect('/admin/user');	
				} else $flashMessage->addMessage(array('error'=>$this->view->translate('admin_save_fail')));
			}		
		}
		$this->view->message = $this->showMessage($flashMessage->getMessages());		
		$this->view->arrRole = $arrUtil->buildArrayWithCustomKey($cUser->getListRole(), 'role_id');
	}
	
	public function editAction() {
		if (!$this->_acl->isUserAllowed('admin:user', 'edit'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
			
		$id = $this->getRequest()->getParam('id', 0);
		if ($id <= 0) {
			$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
			$this->_redirect('/admin/user');
		}
		
		$cUser = new Admin_Model_User();
		$arrUtil = new Core_Utilities_Array();
		if ($this->getRequest()->isPost()) {
			$post = $this->getRequest()->getPost();
			$notEmpty = new Zend_Validate_NotEmpty();
			$isValid = TRUE;
			if (!$notEmpty->isValid($post['jform']['username'])
					|| $cUser->isExistValue($post['jform']['username'])) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['email'])
					|| $cUser->isExistValue($post['jform']['email'], 'email')) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['fullname'])) $isValid = FALSE;
			elseif (!$notEmpty->isValid($post['jform']['role'])) $isValid = FALSE;
			
			if ($isValid) {
				$data = array();
				$data['id'] = $id = $post['jform']['id'];
				$data['username'] = $post['jform']['username'];
				$data['fullname'] = $post['jform']['fullname'];				
				$data['role_id'] = $post['jform']['role'];
				$data['email'] = $post['jform']['email'];
				$data['phone'] = $post['jform']['phone'];
				$data['active'] = $post['jform']['active'];		
				if ($notEmpty->isValid($post['jform']['password']))
					$data['password'] = md5($post['jform']['password']);	
				$response = $cUser->update($data);
				if ($response > 0) {
					$flashMessage->addMessage(array('message'=>$this->view->translate('admin_save_successfull')));
					if ($post['save'] == 'save') $this->_redirect('/admin/user');	
				} else $flashMessage->addMessage(array('error' => $this->view->translate('admin_save_fail')));
			}		
		}
		
		$detail = $cUser->getDetail($id);
		if (empty($detail)) {
			$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_object_notexist')));
			$this->_redirect('/admin/category');
		}
		
		$this->view->detail = $detail;		
		$this->view->message = $this->showMessage($flashMessage->getMessages());		
		$this->view->arrRole = $arrUtil->buildArrayWithCustomKey($cUser->getListRole(), 'role_id');
	}
	
	public function deleteAction() {
		if (!$this->_acl->isUserAllowed('admin:user', 'delete'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
	}
	
	public function activeAction() {
		if (!$this->_acl->isUserAllowed('admin:user', 'edit'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		$id = $this->getRequest()->getParam('uid', 0);
		if ($id <= 0) {
			$ids = $this->getRequest()->getParam('ids', null);
			if (is_null($ids)) {
				$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
				$this->_redirect('/admin/user');
			} else $ids = implode(',', $ids);
		} else $ids = $id;
		$cUser = new Admin_Model_User();
		$response = $cUser->updateStatus(array('active' => 1), $ids); 	
		$this->_redirect('/admin/user');	
	}
	
	public function deactiveAction() {
		if (!$this->_acl->isUserAllowed('admin:user', 'edit'))
			$this->_redirect($this->_config->site->admin->urlAccessDenied);
		$flashMessage = $this->_helper->getHelper('FlashMessenger');
		$id = $this->getRequest()->getParam('uid', 0);
		if ($id <= 0) {
			$ids = $this->getRequest()->getParam('ids', null);
			if (is_null($ids)) {
				$flashMessage->addMessage(array('notice'=>$this->view->translate('admin_id_invalid')));
				$this->_redirect('/admin/user');
			} else $ids = implode(',', $ids);
		} else $ids = $id;
		$cUser = new Admin_Model_User();
		$response = $cUser->updateStatus(array('active' => 0), $ids); 
		$this->_redirect('/admin/user');
	}
}